package com.mdz.sbgl.Controller;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.sbgl.Config.ServerConfig;
import com.mdz.sbgl.Entity.*;
import com.mdz.sbgl.Mapper.SbuserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/sbgl/sbuser")
public class SbuserController {

    @Autowired
    private SbuserMapper sbuserMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private HttpServletRequest request;

    @ResponseBody
    @GetMapping(value = "/login/{gsid}/{loginName}/{loginPassword}")
    public List<Sbuser> login(@PathVariable("gsid") String gsid, @PathVariable("loginName") String loginName, @PathVariable("loginPassword") String loginPassword) {
        String Password = SecureUtil.md5(loginPassword);
        List<Sbuser> user = sbuserMapper.selectList(new QueryWrapper<Sbuser>().eq("login_name", loginName).eq("login_password", Password).eq("gsid",gsid));
        session.setAttribute("username", user.get(0).getUserName());
        session.setAttribute("loginname", user.get(0).getLoginName());
        session.setAttribute("userid", user.get(0).getUserId());
        if (user.get(0).getLoginName().contains("administrator")){
            session.setAttribute("gsid", "");
        }else {
            System.out.println(user.get(0).getGsid());
            session.setAttribute("gsid", user.get(0).getGsid());
        }
        return user;
    }

    @ResponseBody
    @GetMapping(value = "/getsbuser/{gsid}/{loginName}")
    public List<Sbuser> getsbuser (@PathVariable("gsid") String gsid, @PathVariable("loginName") String loginName) {
        List<Sbuser> user = sbuserMapper.selectList(new QueryWrapper<Sbuser>().eq("login_name", loginName).eq("gsid",gsid));
        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/insertsbuser",method = RequestMethod.POST)
    public int insertsbuser(Sbuser sbuser){
        String Password = SecureUtil.md5(sbuser.getLoginPassword());
        sbuser.setLoginPassword(Password);
        return sbuserMapper.insert(sbuser);
    }

    @ResponseBody
    @RequestMapping(value = "/insertsbuserbygly",method = RequestMethod.POST)
    public int insertsbuserbygly(Sbuser sbuser){
        sbuser.setLoginName(sbuser.getGsid());
        sbuser.setUserName(sbuser.getGsid());
        String Password = SecureUtil.md5(sbuser.getLoginPassword());
        sbuser.setLoginPassword(Password);
        return sbuserMapper.insert(sbuser);
    }

    @ResponseBody
    @RequestMapping(value = "/getsbuserlist",method = RequestMethod.GET)
    public String getsbuserlist(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, @RequestParam(value = "cxtj",required = false,defaultValue = "") String cxtj,@RequestParam(value = "gsid",required = false,defaultValue = "") String gsid){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        List<Sbuser> ncList=sbuserMapper.selectList(new QueryWrapper<Sbuser>().like("gsid",gsid).and(wrapper ->wrapper.like("login_name",cxtj).or().like("user_name",cxtj)));
        PageInfo page=new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int)page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/updatesbuser/{uId}", method = RequestMethod.PUT)
    public Integer updatesbuser(Sbuser sbuser, @PathVariable("uId") String uId) {
        return sbuserMapper.update(sbuser, new UpdateWrapper<Sbuser>().eq("user_id",uId));
    }

    @ResponseBody
    @RequestMapping(value = "/resetPwd/{uId}", method = RequestMethod.PUT)
    public Integer resetPwd(Sbuser sbuser,@PathVariable("uId") String uId) {
        String Password = SecureUtil.md5(sbuser.getLoginPassword());
        sbuser.setLoginPassword(Password);
        return sbuserMapper.update(sbuser, new UpdateWrapper<Sbuser>().eq("login_name",uId));
    }
}

